package com.xbongbong.pro.statistic.through.pojo.dto;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.statistic.enums.StatisticsTypeEnum;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.enums.ThroughParamEnum;
import com.xbongbong.pro.statistic.result.pojo.ChartResultBasePojo;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * 穿透所需要的参数pojo
 * @author zcp
 * @version v1.0
 * @date 2019/4/18 13:50
 * @since v1.0
 */
@Getter
@Setter
@ToString
public class ThroughDTO implements Serializable {
    private static final long serialVersionUID = 6102709385570159680L;

    /**
     * 穿透的指标类型（比如表格图表，内部可能有不同的业务需要穿透）
     */
    private SystemCodeEnum systemCodeEnum;

    /**
     * 基础穿透条件，如员工、部门信息、数据字典穿透条件等
     */
    private JSONObject baseThrough;

    /**
     * 只设置systemCodeEnum
     * @param systemCodeEnum
     * @author zcp
     * @date 2019/4/18 15:00
     * @since v1.0
     * @version v1.0
     */
    public ThroughDTO(SystemCodeEnum systemCodeEnum){
        this.systemCodeEnum = SystemCodeEnum.formatPerforOrPk2Normal(systemCodeEnum);
        this.baseThrough = new JSONObject();
    }

    /**
     * 拼装基础穿透条件：refId、员工、部门等
     * @param chartResultPojo
     * @author zcp
     * @date 2019/4/18 15:00
     * @since v1.0
     * @version v1.0
     */
    public ThroughDTO(ChartResultPojo chartResultPojo, SystemCodeEnum systemCodeEnum){
        this.systemCodeEnum = SystemCodeEnum.formatPerforOrPk2Normal(systemCodeEnum);
        this.baseThrough = setBaseThrough(chartResultPojo, chartResultPojo.getCompanyStructType(), chartResultPojo.getCheckedDepId(), chartResultPojo.getCheckedUserId());
    }

    /**
     * 拼装基础穿透条件,自定义指标用
     * @param chartResultBasePojo
     * @author xinpeng.jiang
     * @date 2020/05/12
     * @since v1.0
     * @version v1.0
     */
    public ThroughDTO(ChartResultBasePojo chartResultBasePojo) {
        this.baseThrough = setBaseThrough(chartResultBasePojo, chartResultBasePojo.getCompanyStructType(), chartResultBasePojo.getCheckedDepId(), chartResultBasePojo.getCheckedUserId());
    }

    /**
     * 资金相关穿透条件
     *
     * @param chartResultPojo 获取图表结果参数pojo
     * @param systemCodeEnum 系统图表枚举
     * @param accountId 资金账户id
     * @author 徐俊杰
     * @date 2020/3/24 15:04
     * @since v1.0
     */
    public ThroughDTO(ChartResultPojo chartResultPojo, SystemCodeEnum systemCodeEnum, Long accountId){
        this.systemCodeEnum = SystemCodeEnum.formatPerforOrPk2Normal(systemCodeEnum);
        this.baseThrough = setBaseThrough(chartResultPojo, chartResultPojo.getCompanyStructType(), chartResultPojo.getCheckedDepId(), chartResultPojo.getCheckedUserId());
        this.baseThrough.put("accountId", accountId);
    }

    /**
     * pk榜穿透条件：refId、员工、部门、systemCodeEnum等
     * @param chartResultBasePojo pk榜统计参数pojo
     * @param checkedDepId 穿透部门id
     * @param checkedUserId 穿透员工id
     * @param systemCodeEnum 系统指标枚举
     * @author zcp
     * @date 2019/4/24 15:11
     * @since v1.0
     * @version v1.0
     */
    public ThroughDTO(ChartResultBasePojo chartResultBasePojo, Long checkedDepId, String checkedUserId, SystemCodeEnum systemCodeEnum){
        this.systemCodeEnum = SystemCodeEnum.formatPerforOrPk2Normal(systemCodeEnum);
        //处理其他穿透信息
        this.baseThrough = setBaseThrough(chartResultBasePojo, chartResultBasePojo.getCompanyStructType(), checkedDepId, checkedUserId);
    }

    /**
     * 设置数据字典code
     * @param code
     * @author zcp
     * @date 2019/4/22 19:50
     * @since v1.0
     * @version v1.0
     */
    public void setDictionaryCodeThrough(String code) {
        if (baseThrough == null) {
            baseThrough = new JSONObject();
        }
        baseThrough.put(ThroughParamEnum.DICTIONARY_CODE.getKey(), code);
    }

    /**
     * 设置基础穿透信息：组织架构类型、员工id、部门id、refId、province等
     * @param chartResultBasePojo 请求参数pojo
     * @param companyStructType 组织架构筛选类型
     * @param checkedDepId 部门id
     * @param checkedUserId 员工id
     * @return com.alibaba.fastjson.JSONObject
     * @author zcp
     * @date 2019/4/25 10:15
     * @since v1.0
     * @version v1.0
     */
    private JSONObject setBaseThrough(ChartResultBasePojo chartResultBasePojo, Integer companyStructType, Long checkedDepId, String checkedUserId) {
        //处理其他穿透信息
        JSONObject throughObject = new JSONObject();
        //筛选的员工id、部门id
        throughObject.put(ThroughParamEnum.COMPANY_STRUCT_TYPE.getKey(), companyStructType);
        throughObject.put(ThroughParamEnum.CHECKED_DEP_ID.getKey(), checkedDepId);
        throughObject.put(ThroughParamEnum.CHECKED_USER_ID.getKey(), checkedUserId);
        Boolean singleCheck = chartResultBasePojo.getSingleCheck();
        if (Objects.isNull(singleCheck) || !singleCheck) {
            //只有强制单选的才不需要塞入这几个多选时使用的参数
            throughObject.put(ThroughParamEnum.CHECKED_DEP_ID_IN.getKey(), chartResultBasePojo.getCheckedDepIdIn());
            throughObject.put(ThroughParamEnum.CHECKED_USER_ID_IN.getKey(), chartResultBasePojo.getCheckedUserIdIn());
            throughObject.put(ThroughParamEnum.CHECKED_ID.getKey(), chartResultBasePojo.getCheckedId());
        }
        
        Long refId = chartResultBasePojo.getRefId();
        if (refId != null && refId != -1) {
            throughObject.put(ThroughParamEnum.REF_ID.getKey(), refId);
        }
        Long argTwoId = chartResultBasePojo.getArgTwoId();
        if (Objects.nonNull(argTwoId) && argTwoId > 0) {
            throughObject.put(ThroughParamEnum.ARG_TWO_ID.getKey(), argTwoId);
        }
        List<String> argStrIn = chartResultBasePojo.getArgStrIn();
        if (CollectionUtils.isNotEmpty(argStrIn)) {
            throughObject.put(ThroughParamEnum.ARG_STR_IN.getKey(), argStrIn);
        }
        String province = chartResultBasePojo.getProvince();
        if (!StringUtil.isEmpty(province)) {
            throughObject.put(ThroughParamEnum.PROVINCE.getKey(), province);
        }
        Boolean performancePk = chartResultBasePojo.getPerformancePk();
        if (performancePk != null) {
            throughObject.put(ThroughParamEnum.PERFORMANCE_PK.getKey(), performancePk);
        }
        ChartEntity chartEntity = chartResultBasePojo.getChartEntity();
        if (chartEntity != null) {
            //塞入图表的id和所属的（二级）分类id
            throughObject.put(ThroughParamEnum.id.getKey(), chartEntity.getId());
            throughObject.put(ThroughParamEnum.STATISTICS_TYPE.getKey(), chartEntity.getStatisticsType());
            List<Long> categoryIdIn = JsonHelperUtil.parseArray(String.valueOf(chartEntity.getCategory()), Long.class);
            if (categoryIdIn.size() > 0) {
                throughObject.put(ThroughParamEnum.CATEGORY_ID.getKey(), categoryIdIn.get(0));
            }
        } else {
            if (systemCodeEnum != null && !Objects.equals(systemCodeEnum, SystemCodeEnum.UNKNOWN)) {
                throughObject.put(ThroughParamEnum.STATISTICS_TYPE.getKey(), StatisticsTypeEnum.SYSTEM_STATISTIC.getCode());
            } else {
                throughObject.put(ThroughParamEnum.STATISTICS_TYPE.getKey(), StatisticsTypeEnum.CUSTOM_STATISTIC.getCode());
            }
        }
        return throughObject;
    }
}
